package slap.server;

import java.util.Map;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;

import slap.concurrent.Invoker;
import slap.http.Request;
import slap.http.websocket.Inbound;
import slap.http.websocket.Outbound;
import slap.mvc.WebSocketInvoker;

public class WebSocketInvocation extends Invoker.Invocation {
	protected static final Logger logger = LoggerFactory.getLogger(WebSocketInvocation.class);
	Request request;
	Inbound inbound;
	Outbound outbound;
	ChannelHandlerContext ctx;
	MessageEvent e;
	
	public WebSocketInvocation(Request request,
			Inbound inbound, Outbound outbound,
			ChannelHandlerContext ctx, MessageEvent e) {
		this.request = request;
		this.inbound = inbound;
		this.outbound = outbound;
		this.ctx = ctx;
		this.e = e;
	}

	@Override
	public boolean init() {
		Request.setCurrent(request);
		Inbound.current.set(inbound);
		Outbound.current.set(outbound);
		return super.init();
	}


	@Override
	public void execute() throws Exception {
		WebSocketInvoker.invoke(request, inbound, outbound);
	}

	@Override
	public void onException(Throwable e) {
		logger.error(
				"Internal Server Error in WebSocket (closing the socket) for request {} {}",
				request.method(),  request.path());
		logger.equals(e);
		ctx.getChannel().close();
		super.onException(e);
	}

	@Override
	public void onSuccess() throws Exception {
		//outbound.close();
		super.onSuccess();
	}
}